package com.liu.demo1.testDemo;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * @author liusiyu
 * @ClassName Thread
 * @date 2022年01月12日 15:20
 */
public class MyThread {

    public static void main(String[] args) {
        God god = new God();
        You you = new You();
        Thread t1 = new Thread(god);
        t1.setDaemon(true);
        t1.start();
        new Thread(you).start();
    }
}

class God implements Runnable{

    @Override
    public void run() {
        while (true)
        System.out.println("--上帝与你同在--");
    }
}

class You implements Runnable{

    @Override
    public void run() {
        for (int i = 0; i < 36500; i++) {
            System.out.println("开心每一天");
        }
        System.out.println("goodBye，world！");
    }
}
// -------------------------------------------------
class unSafeList{
    @Test
    public static void main(String[] args) throws InterruptedException {
        List<String> list = new ArrayList<>();
        //线程安全的list
        CopyOnWriteArrayList<String> list1 = new CopyOnWriteArrayList<>();
        Runnable runnable = new Runnable() {
            @Override
            public synchronized void run() {
                list.add(Thread.currentThread().getName());
            }
        };
        for (int i = 0; i < 10000; i++) {
            new Thread(runnable).start();
        }
        System.out.println(list.size());
    }
}

class Threadpool{
    public static void main(String[] args) {
        Executor executor = Executors.newFixedThreadPool(10);
        for (int i = 0; i < 12; i++) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName());
                }
            });
        }
    }
}
// -------------------------------------------------
